<?php
//自动生成,请根据需要修改
namespace app\activity\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;

/**
 * @title 活动页面（模块09-01）
 * @description 活动说明
 * @group 活动（09）
 * @header name:model_id require:0 default:09-01 desc:模块
 * @groupremark 已使用错误码：1-09-01-0001 校验失败 1-09-01-0002 添加失败 1-09-01-0003 编辑校验失败 1-09-01-0004 编辑失败 1-09-01-0005 获取详情失败  1-09-01-0006 获取列表失败 1-09-01-0007获取单个详情失败 1-09-01-0008 删除失败
 */
class Activity extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加活动
     * @description 添加活动
     * @author 李正
     * @url /activity/activity/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:time type:varchar require:0 other: desc:活动时间
     * @param name:address type:varchar require:0 other: desc:活动地点
     * @return data:活动@!
     * @data    id:id    title:标题    content:内容    create_time:创建时间    over_time:过期时间    status:状态    time:活动时间    address:活动地点
     * @return error_id:1-09-01-0001 校验失败  1-09-01-0002 添加失败
     */
    public function add()
    {
        $data = input();
        $data['create_time'] = time();

        /**
         *$validate = new \app\activity\validate\Activity();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-09-01-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\activity\model\Activity();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-09-01-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑活动
     * @description 编辑活动
     * @author 李正
     * @url /activity/activity/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:time type:varchar require:0 other: desc:活动时间
     * @param name:address type:varchar require:0 other: desc:活动地点
     * @return data:活动@!
     * @data    id:id    title:标题    content:内容    create_time:创建时间    over_time:过期时间    status:状态    time:活动时间    address:活动地点
     * @return error_id:1-09-01-0003 校验失败 1-09-01-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\activity\validate\Activity();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-09-01-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\activity\model\Activity();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-09-01-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除活动
     * @description 删除活动
     * @author 李正
     * @url /activity/activity/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-09-01-0008 删除失败
     */
    public function delete()
    {
        Db::startTrans();
        $id = input('id');
        $model = new \app\activity\model\Activity();
        $flag = $model->destroy($id);
        $apdel = Db::table('zb_activity.tp_activity_participate')->where('activity_id','=',$id)->delete();
        if($apdel === 0){
            $apdel = true;
        }
        if ($flag && $apdel){
            //提交事件
            Db::commit();
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        }else{
            //回滚事务
            Db::rollback();
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-09-01-0008';
            return $this->errorJson($msg);
        }

    }

    /**
     * @title 单个查询活动
     * @description 单个查询活动
     * @author 李正
     * @url /activity/activity/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:time type:varchar require:0 other: desc:活动时间
     * @param name:address type:varchar require:0 other: desc:活动地点
     * @return data:活动@!
     * @data    id:id    title:标题    content:内容    create_time:创建时间    over_time:过期时间    status:状态    time:活动时间    address:活动地点
     * @return error_id:1-09-01-0005 校验失败 1-09-01-0007 获取失败
     */
    public function info()
    {
        $model = new \app\activity\model\Activity();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\activity\validate\Activity();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-09-01-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\activity\model\Activity();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-09-01-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表活动
     * @description 查询列表活动
     * @author 李正
     * @url /activity/activity/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:活动@
     * @list    id:id    title:标题    content:内容    create_time:创建时间    over_time:过期时间    status:状态    time:活动时间    address:活动地点
     * @return error_id:1-09-01-0006 校验失败
     */
    public function get_list()
    {
        $model = new \app\activity\model\Activity();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        foreach($data as $k => $v){
            if($v === 0){
                continue;
            }
            if($v == null){
                unset($data[$k]);
            }
        }
        if(isset($data['title']) && $data['title'] && !empty($data['title'])){
            $data['title'] = ['like',"%{$data['title']}%"];
        }
        if(isset($data['status']) && $data['status'] && !empty($data['status'])){
            $data['status'] = ['=',"{$data['status']}"];
        }
        if(isset($data['time']) && $data['time'] && !empty($data['time'])){
            $data['time'] = ['like',"%{$data['time']}%"];
        }
        if(isset($data['address']) && $data['address'] && !empty($data['address'])){
            $data['address'] = ['=',"{$data['address']}"];
        }
        /**
         *$validate = new \app\activity\validate\Activity();
         *$validate->scene('lists');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-09-01-0006';
         *return $this->errorJson($msg);
         *}
         **/
        $data = new Where($data);
        $list = $model->where($data)->order('id','desc')->paginate();
        $msg['list'] = $list;
        return $this->successJson($msg);
    }

    /**
     * @title 前台活动介绍页面
     * @description 前台活动介绍页面
     * @author 李正
     * @url /activity/activity/activity_introduce
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @method *
     */
    public function activity_introduce()
    {
        $activity = Db::name('activity')
            ->order('id','desc')
            ->where('status',1)
            ->find();

        $activity_list = Db::name('activity')
            ->order('id','desc')
            ->where('status',1)
            ->paginate();

        $result['activity'] = $activity;
        $result['activity_list'] = $activity_list;
        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * 后台轮播图启用按钮
     */
    public function invoke()
    {
        $data = input();

        Db::name('activity')
            ->where('id',$data['id'])
            ->setField('status',1);

        $msg['msg'] = '轮播图启用成功';
        return $this->successJson($msg);
    }

    /**
     * 后台轮播图禁用按钮
     */
    public function forbidden()
    {
        $data = input();

        Db::name('activity')
            ->where('id',$data['id'])
            ->setField('status',0);

        $msg['msg'] = '轮播图禁用成功';
        return $this->successJson($msg);
    }
}
